<?php
class DNN_model extends CI_Model 
{
	protected $_table   = '';
	protected $_language = 'en';
	
	function __construct() {
		parent::__construct();
		$_language = $this->session->userdata('language') == "english" ? "en" : "vi";
	}
	
	function add($data)
	{
		$this->db->insert($this->_table,$data);
		return $this->db->insert_id();
	}
	
	function getID($id)
	{
		$query = $this->db->get_where($this->_table, array('id' => $id));
		if($query->num_rows()> 0)
			return $query->row_array();
		else
			return false;
	}
	function update($id, $data) {
		$this->db->where('id', $id);
		$this->db->update($this->_table, $data);
	}
	function delete($id)
	{
		$this->db->where('id', $id);
		$this->db->delete($this->_table);
	}
	function count_by_tid($tid, $lang = null) {
		$this->db->select('count(*) as count_value');
		$this->db->from($this->_table);
		if ($tid != null && $tid != 0) {
			$this->db->where('tid', $tid);;
		}
		if ($lang != null && $lang != '') {
			$this->db->where('language', $lang);
		}
		$row = $this->db->get()->row();
		if ($row !== null) {
			return $row->count_value;
		} else {
			return 0;
		}
	}
	function get_all() {
		$this->db->order_by('id','asc');
		return $this->db->get($this->_table);
	}
	function count_all() {
		return $this->db->count_all($this->_table);
	}
	function count_by_attr($column, $value) {
		$this->db->select('count(*) as count_value');
		$this->db->from($this->_table);
		$this->db->where("$column", $value);;
		$row = $this->db->get()->row();
		if ($row !== null) {
			return $row->count_value;
		} else {
			return 0;
		}
	}
}